/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell.value;

import org.dom4j.Element;

/**
 * 公式对象
 * 
 * @version 1.0 2011-4-4
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class FormulaObject extends AbstractCellObject {
	/**
	 * 公式值
	 */
	private String formula = "";

	/**
	 * 构造器
	 */
	public FormulaObject() {
		this("");
	}

	/**
	 * 构造器
	 * 
	 * @param formula
	 *            公式
	 */
	public FormulaObject(String formula) {
		this.formula = formula;
	}

	/**
	 * 获取公式值
	 * 
	 * @return 工时值
	 */
	public String getFormula() {
		if (formula.startsWith("=")) {
			return formula.substring(1);
		} else {
			return formula;
		}
	}

	public void setFormula(String formula) {
		if (formula.startsWith("=")) {
			this.formula = formula.substring(1);
		} else {
			this.formula = formula;
		}
	}

	@Override
	public String toString() {
		if (this.formula == null) {
			return "";
		}

		if (this.formula.startsWith("=")) {
			return this.formula;
		} else {
			return "=" + this.formula;
		}
	}

	@Override
	public FormulaObject clone() {
		return (FormulaObject) super.clone();
	}

	@Override
	public void read(Element parent) {
		this.setFormula(parent.getTextTrim());
	}

	@Override
	public void write(Element parent) {
		parent.addAttribute("class", ModelDesignableValueObjectClassManager.INSTANCE.getClassKey(this.getClass()));

		parent.addText(this.formula);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((formula == null) ? 0 : formula.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FormulaObject other = (FormulaObject) obj;
		if (formula == null) {
			if (other.formula != null)
				return false;
		} else if (!formula.equals(other.formula))
			return false;
		return true;
	}
}
